package templatemethod.strategy;

/**
 * @author wusd
 * @date 2020/2/10 22:17
 */
public class BubbleSorter {
    private int operations = 0;
    private int length = 0;
    private SortHandle itsSortHandle = null;
    public BubbleSorter(SortHandle handle) {
        itsSortHandle = handle;
    }

    public int sort(Object array) {
        itsSortHandle.setArray(array);
        length = itsSortHandle.length();
        operations = 0;
        if (length <= 1) return operations;

        for (int nextToLast = length - 2; nextToLast >= 0; nextToLast--) {
            for (int index = 0; index <= nextToLast; index++) {
                if (itsSortHandle.outOfOrder(index))
                    itsSortHandle.swap(index);
                operations++;
            }
        }
        return operations;
    }
}
